using Godot;
using Godot.Collections;


public class Tower : Node2D
{

    [Signal]
    delegate void FarestEmeryDetected(Emery emery);

    public const int LEVEL_ONE = 1;
    public const int LEVEL_TWO = 2;
    public const int LEVEL_THREE = 3;
    

    [Export(PropertyHint.Range, "1,3")]
    public int Level = LEVEL_ONE;

    private Timer _scanTimer;

    private Area2D _emeryDetector;

    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        _scanTimer = GetNode<Timer>("ScanTimer");
        _scanTimer.Connect("timeout", this, "OnScanTimeout");

        // EmeryDetector/DetectorShap
        CollisionShape2D d2d = GetNode<CollisionShape2D>("EmeryDetector/DetectorShap");
        if (d2d != null)
        {
            CircleShape2D cc = new CircleShape2D();

            switch (Level)
            {
                case LEVEL_ONE:
                    cc.Radius = 64;
                    break;
                case LEVEL_TWO:
                    cc.Radius = 80;
                    break;
                case LEVEL_THREE:
                    cc.Radius = 100;
                    break;
                default:
                    break;
            }
            d2d.Shape = cc;
        }

        TowerIndicator indicator = GetNode<TowerIndicator>("TowerIndicator");
        if (indicator != null)
        {
            switch(Level)
            {
                case LEVEL_ONE:
                    indicator.Radius = 64;
                    break;
                case LEVEL_TWO:
                    indicator.Radius = 80;
                    break;
                case LEVEL_THREE:
                    indicator.Radius = 100;
                    break;
            }
        }

        ////////////////////////////////////////////////////////////////
        // 检测敌人

        _emeryDetector = GetNode<Area2D>("EmeryDetector");
        if (_emeryDetector != null)
        {
            _emeryDetector.Connect("area_entered", this, "OnAreaEntered");
            _emeryDetector.Connect("area_exited", this, "OnAreaExited");
        }

        ////////////////////////////////////////////////////////////////
        // 接收信号，发起攻击

        Connect("FarestEmeryDetected", this, "AttackEmery");
    }

    public void AttackEmery(Emery em)
    {
#if DEBUG
        GD.Print("Need Attack Emery: ", em.Name);
#endif
    }

    public void OnScanTimeout()
    {
        Emery em = DetectFarestEmery();
        if (em != null)
        {
            GD.Print("Farest Emery: ", em.Name);
            EmitSignal("FarestEmeryDetected", em);
        }
    }

    private Emery DetectFarestEmery()
    {

        Emery result = null;
        float max = 0;
        // TODO: 检测覆盖上的区域，发现敌人
        Array areas = _emeryDetector.GetOverlappingAreas();
        if (areas != null && areas.Count > 0)
        {
            foreach (var area in areas)
            {
                if (area is Area2D)
                {
                    Area2D a2d = area as Area2D;
                    if (a2d.IsInGroup("Emery"))
                    {
                        Emery em = a2d.GetParent() as Emery;
                        GD.Print("Detected: Emery: ", em.Name);

                        // Tower pos -> emery pos
                        float dis = Position.DistanceTo(em.Position);
                        if(dis > max)
                        {
                            result = em;
                            max = dis;
                        }
                    }
                }
            }
        }

        return result;
    }

    public void OnAreaEntered(Area2D area)
    {
        if(area.IsInGroup("Emery"))
        {
            Emery em = area.GetParent() as Emery;
            GD.Print("Emery Entered: ", em.Name);

            if (_scanTimer.IsStopped())
            {
                _scanTimer.Start();
            }
        }
    }

    public void OnAreaExited(Area2D area)
    {

    }

}
